def check_dependencies():
    """检查并提示安装必要的依赖"""
    try:
        import rdflib, translatepy
    except ImportError:
        print("错误：缺少必要的依赖库。")
        print("请运行以下命令安装依赖：")
        print("pip install rdflib translatepy")
        return False
    return True

if not check_dependencies():
    import sys
    sys.exit(1)

import datetime
import json
import ssl
import urllib.request
import re
from rdflib import Graph
from translatepy import Translator

# 初始化翻译器
translator = Translator()

# 初始化 SSLContext
ctx = ssl.create_default_context()

# 初始化 RDF 图对象
graph = Graph()

# 获取当前日期
date = datetime.datetime.now()

# 预处理函数：替换无效日期字符串
def preprocess_rdf_data(raw_data):
    invalid_date_pattern = r'(-?\d{4,7}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)'
    
    def replace_invalid_date(match):
        date_str = match.group(1)
        try:
            datetime.datetime.fromisoformat(date_str.replace('Z', ''))
            return date_str
        except ValueError:
            return "0001-01-01T00:00:00Z"
    
    processed_data = re.sub(invalid_date_pattern, replace_invalid_date, raw_data)
    return processed_data

def translate_to_english(text):
    """将中文翻译成英文"""
    try:
        translation = translator.translate(text, "English").result
        return translation
    except Exception as e:
        print(f"翻译出错: {str(e)}")
        return text  # 翻译失败返回原文本

def process_data(graph, lang, specials):
    try:
        prefixes = []
        suffixes = []
        apprentices = 0
        kits = 0

        query = graph.query(f'''
            SELECT ?translation ?suffix WHERE {{
                ?cat wdt:P3 wd:Q622 ;
                     p:P84 ?name .
                ?name pq:P85 ?translation ;
                      pq:P111 ?suffix .
                FILTER (lang(?translation) = "{lang}")
            }}
        ''')

        results = list(query)
        print(f"找到 {len(results)} 条{lang}语言记录")

        for row in results:
            try:
                translation_value = str(row.translation)
                suffix_value = str(row.suffix) if row.suffix else ""

                if translation_value in specials:
                    prefix, suffix = specials[translation_value]
                    prefixes.append(prefix)
                    suffixes.append(suffix)
                else:
                    if suffix_value == 'paw':
                        if len(translation_value) > 0:
                            prefixes.append(translation_value[:-1])
                            apprentices += 1
                    elif suffix_value == 'kit':
                        if lang.startswith('zh'):
                            if translation_value.endswith('崽'):
                                prefixes.append(translation_value[:-1])
                            else:
                                prefixes.append(translation_value)
                        else:
                            prefixes.append(translation_value.replace('kit', '').strip())
                        kits += 1
                    else:
                        if len(translation_value) > 0:
                            prefixes.append(translation_value[:-1])
                            suffixes.append(translation_value[-1])
            except Exception as e:
                print(f"处理行数据时出错: {str(e)}")
                continue

        # 去重处理
        unique_prefixes = list(set(prefixes))
        unique_suffixes = list(set(suffixes))
        
        return {
            'language': lang,
            'date': {
                'year': date.year,
                'month': date.month,
                'day': date.day
            },
            'prefixes': unique_prefixes,
            'suffixes': unique_suffixes,
            'apprentices': apprentices,
            'kits': kits
        }
    except Exception as e:
        print(f"处理{lang}语言数据时出错: {str(e)}")
        return None

def generate_english_data(zh_data):
    """从中文数据生成英文翻译"""
    try:
        print("\n开始生成英文翻译数据...")
        
        # 翻译前缀
        en_prefixes = []
        print(f"需要翻译 {len(zh_data['prefixes'])} 个前缀...")
        for i, prefix in enumerate(zh_data['prefixes']):
            try:
                translated = translate_to_english(prefix)
                en_prefixes.append(translated)
                if i % 50 == 0:  # 每50个打印一次进度
                    print(f"已翻译 {i+1}/{len(zh_data['prefixes'])}: {prefix} -> {translated}")
            except Exception as e:
                print(f"翻译前缀 '{prefix}' 时出错: {str(e)}")
                en_prefixes.append(prefix)  # 出错时保留原中文
        
        # 翻译后缀
        en_suffixes = []
        print(f"\n需要翻译 {len(zh_data['suffixes'])} 个后缀...")
        for i, suffix in enumerate(zh_data['suffixes']):
            try:
                translated = translate_to_english(suffix)
                en_suffixes.append(translated)
                if i % 20 == 0:  # 每20个打印一次进度
                    print(f"已翻译 {i+1}/{len(zh_data['suffixes'])}: {suffix} -> {translated}")
            except Exception as e:
                print(f"翻译后缀 '{suffix}' 时出错: {str(e)}")
                en_suffixes.append(suffix)  # 出错时保留原中文
        
        print(f"\n翻译完成！共翻译 {len(en_prefixes)} 个前缀和 {len(en_suffixes)} 个后缀")
        
        return {
            'language': 'en',
            'date': zh_data['date'],
            'prefixes': en_prefixes,
            'suffixes': en_suffixes,
            'apprentices': zh_data['apprentices'],
            'kits': zh_data['kits']
        }
    except Exception as e:
        print(f"生成英文数据时出错: {str(e)}")
        return None

def main():
    try:
        # 加载 RDF 数据
        url = 'https://raw.gitcode.com/ryj/dummp/raw/master/wbdump.ttl'
        print(f"正在从 {url} 下载数据...")
        
        try:
            with urllib.request.urlopen(url, context=ctx) as dump:
                raw_data = dump.read().decode('utf-8')
                print("数据下载成功！")
        except Exception as e:
            print(f"下载数据失败: {str(e)}")
            print("请检查网络连接或URL是否可访问")
            return

        # 预处理 RDF 数据
        print("正在预处理数据...")
        preprocessed_data = preprocess_rdf_data(raw_data)
        
        # 加载预处理后的数据
        print("正在解析RDF数据...")
        try:
            graph.parse(data=preprocessed_data, format='turtle')
            print("RDF数据解析成功！")
        except Exception as e:
            print(f"RDF数据解析失败: {str(e)}")
            return

        # 特殊情况处理
        specials_CN = {'桦树皮': ('桦', '树皮')}
        specials_TW = {'樺樹皮': ('樺', '樹皮')}

        # 处理每种语言的数据
        languages = [
            ('zh-cn', specials_CN, '简体中文'),
            ('zh-tw', specials_TW, '繁体中文')
        ]

        # 创建输出目录
        output_dir = 'data/'
        import os
        os.makedirs(output_dir, exist_ok=True)
        print(f"创建输出目录: {output_dir}")

        # 处理中文数据
        zh_data = None
        for lang_code, special_cases, lang_name in languages:
            print(f"\n处理{lang_name}数据...")
            data = process_data(graph, lang_code, special_cases)
            if data:
                output_file = f'{output_dir}{lang_code}.js'
                try:
                    with open(output_file, 'w', encoding='utf-8') as fw:
                        fw.write('const data = ' + json.dumps(data, ensure_ascii=False))
                    print(f"{lang_name}数据已保存到: {output_file}")
                    print(f"找到 {len(data['prefixes'])} 个前缀和 {len(data['suffixes'])} 个后缀")
                    
                    # 保存简体中文数据用于生成英文翻译
                    if lang_code == 'zh-cn':
                        zh_data = data
                except Exception as e:
                    print(f"保存{lang_name}数据时出错: {str(e)}")
            else:
                print(f"警告：{lang_name}数据处理失败，跳过文件写入")

        # 生成英文翻译数据
        if zh_data:
            en_data = generate_english_data(zh_data)
            if en_data:
                output_file = f'{output_dir}en.js'
                with open(output_file, 'w', encoding='utf-8') as fw:
                    fw.write('const data = ' + json.dumps(en_data, ensure_ascii=False))
                print(f"\n英文翻译数据已保存到: {output_file}")

        print("\n处理完成！")
        
    except Exception as e:
        print(f"发生错误: {str(e)}")
        print("请检查网络连接和必要的依赖是否正确安装")

if __name__ == "__main__":
    main()